﻿using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using API;
using CoreGraphics;
using Foundation;
using mb;
using mb.iOS;
using Masonry;
using MyExtensions;
using UIKit;

namespace libmp
{
    public class VCSetting : UIViewController
    {
        ILog logger = mb.ServiceLocator.Instance.Get<ILog>();

        public VCSetting()
        {
            TabBarItem = new UITabBarItem("设置", null, 0);
        }
        public static bool ifLoggin { set; get; }
        UIView bkView;

        public override void ViewDidLoad()
        {
            bkView = new UIView() { BackgroundColor = Util.MainBKColor };
            bkView.SetParent(View).MakeConstraints(m =>
            {
                m.Top.EqualTo(this.TopLayoutGuideBottom());
                m.Left.Right.Bottom.EqualTo(View);
            });
            base.ViewDidLoad();

            createHeader();
            CreateBalance();
            CreateRecharge();
            CreateOperation();
        }
        public override void ViewWillAppear(bool animated)
        {
            logger.Log("再次进入");
            ResetUserInfo();
        }

        UIView headerView = new UIView();

        UIView headerBottomView;
        private void createHeader()
        {

            headerView.SetParent(bkView);
            headerView.MakeConstraints(m =>
            {
                m.Left.EqualTo(bkView);
                m.Top.EqualTo(bkView);
                m.Right.EqualTo(bkView);
                m.Height.EqualTo(NSObject.FromObject(170));
            });
            headerView.BackgroundColor = UIColor.FromRGB(251, 114, 153);

            createHeaderBottomView();
        }

        private void createHeaderBottomView()
        {
            headerBottomView = new UIView();
            headerBottomView.SetParent(headerView);
            headerBottomView.MakeConstraints(m =>
            {
                m.Left.EqualTo(headerView);
                m.Top.EqualTo(NSObject.FromObject(30));
                m.Right.EqualTo(headerView);
                m.Height.EqualTo(NSObject.FromObject(100));
            });
            logger.Log("User.me.token:" + User.me.token);
            if (User.me.token.Length > 0)
            {
                createHeaderBottomLogged(headerBottomView);
            }
            else
            {
                createHeaderBottomNoLogin(headerBottomView);
            }
        }
        public static UIView shadowView { get; set; }
        private void createHeaderBottomNoLogin(UIView headerBottomView)
        {
            var btnWidth = 125;
            var btnHeight = 50;
            var headerBottomLeftView = new UIView();
            headerBottomLeftView.SetParent(headerBottomView);
            headerBottomLeftView.MakeConstraints(m =>
            {
                m.Left.EqualTo(headerBottomView);
                m.Top.EqualTo(NSObject.FromObject(20));
                m.Bottom.EqualTo(headerBottomView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width / 2)));
            });

            var registerButtonView = new UIView();
            registerButtonView.SetParent(headerBottomLeftView);
            registerButtonView.MakeConstraints(m =>
            {
                m.CenterX.EqualTo(headerBottomLeftView.CenterX());
                m.Top.EqualTo(NSObject.FromObject(5));
                m.Height.EqualTo(NSObject.FromObject(btnHeight));
                m.Width.EqualTo(NSObject.FromObject(btnWidth));
            });
            var registerButton = new UIButton();
            registerButton.Frame = new CGRect(0, 0, btnWidth, btnHeight);
            registerButton.SetParent(registerButtonView);
            var registerImageViewButton = new UIImageView();
            var imgRegister = UIImage.FromFile($"{App.HotStorageRoot}/res/user/Register.png");
            registerImageViewButton.Image = new UIImage(imgRegister.CGImage, 2, UIImageOrientation.Up);
            registerImageViewButton.SetParent(registerButton);
            registerImageViewButton.MakeConstraints(m =>
            {
                m.CenterX.EqualTo(headerBottomLeftView.CenterX());
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(btnHeight));
                m.Width.EqualTo(NSObject.FromObject(btnWidth));
            });
            registerButton.TouchUpInside += (s, e) =>
            {
                logger.Log("注册了1111");
                // var registerAltView = new RegisterAltViewController(this);
                // registerAltView.ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen;      
                // shadowView = new UIView(View.Bounds);      
                // shadowView.BackgroundColor = UIColor.Black;      
                // shadowView.Alpha = 0.5f;      
                // this.View.AddSubview(shadowView);
                // PresentViewController(registerAltView, true, null);
                AppDelegate.Instance.NavController.PushViewController(new VCRegisterController(this), false);
            };

            var headerBottomRightView = new UIView();
            headerBottomRightView.SetParent(headerBottomView);
            headerBottomRightView.MakeConstraints(m =>
            {
                m.Left.EqualTo(headerBottomLeftView.Right());
                m.Top.EqualTo(headerBottomLeftView);
                m.Bottom.EqualTo(headerBottomLeftView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width / 2)));
            });

            var loginButtonView = new UIView();
            loginButtonView.SetParent(headerBottomRightView);
            loginButtonView.MakeConstraints(m =>
            {
                m.CenterX.EqualTo(headerBottomRightView.CenterX());
                m.Top.EqualTo(NSObject.FromObject(5));
                m.Height.EqualTo(NSObject.FromObject(btnHeight));
                m.Width.EqualTo(NSObject.FromObject(btnWidth));
            });
            var loginButton = new UIButton();
            loginButton.Frame = new CGRect(0, 0, btnWidth, btnHeight);
            loginButton.SetParent(loginButtonView);
            var loginImageViewButton = new UIImageView();
            var imgLogin = UIImage.FromFile($"{App.HotStorageRoot}/res/user/Signin.png");
            loginImageViewButton.Image = new UIImage(imgLogin.CGImage, 2, UIImageOrientation.Up);
            loginImageViewButton.SetParent(loginButton);
            loginImageViewButton.MakeConstraints(m =>
            {
                m.CenterX.EqualTo(headerBottomRightView.CenterX());
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(btnHeight));
                m.Width.EqualTo(NSObject.FromObject(btnWidth));
            });

            loginButton.TouchUpInside += (s, e) =>
            {
                logger.Log("登录了1");
                openLoginController();
            };
        }

        public void openLoginController()
        {
            // var loginAltView = new LoginAltViewController(this);
            // loginAltView.ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen;      
            // shadowView = new UIView(View.Bounds);      
            // shadowView.BackgroundColor = UIColor.Black;      
            // shadowView.Alpha = 0.5f;      
            // this.View.AddSubview(shadowView);
            // PresentViewController(loginAltView, true, null);
            AppDelegate.Instance.NavController.PushViewController(new VCLoginController(this), false);
        }

        private void createHeaderBottomLogged(UIView headerBottomView)
        {
            var UserLevelArray = new string[]
            {
                "user/member1.png",
                "user/member2.png",
                "user/member3.png",
                "user/member4.png"
            };
            var logoWidth = 75;
            var userInfoView = new UIView();
            userInfoView.SetParent(headerBottomView);
            //userInfoView.BackgroundColor = UIColor.Red;
            userInfoView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(15));
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Right.EqualTo(NSObject.FromObject(-10));
                m.Height.EqualTo(NSObject.FromObject(logoWidth));
            });

            var userLogoImageViewButton = new UIImageView();
            var imgRecharge = UIImage.FromFile($"{App.HotStorageRoot}/res/user/avatar.png");
            userLogoImageViewButton.Image = new UIImage(imgRecharge.CGImage, 2, UIImageOrientation.Up);
            userLogoImageViewButton.Layer.CornerRadius = logoWidth / 2;
            userLogoImageViewButton.ClipsToBounds = true;
            userLogoImageViewButton.SetParent(userInfoView);
            userLogoImageViewButton.Layer.BorderWidth = 2;
            userLogoImageViewButton.Layer.BorderColor = new CGColor(50, 50, 50);
            userLogoImageViewButton.MakeConstraints(m =>
            {
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(logoWidth));
                m.Width.EqualTo(NSObject.FromObject(logoWidth));
            });
            var logoButton = new UIButton();
            logoButton.SetParent(userInfoView);
            logoButton.MakeConstraints(m =>
            {
                m.Top.EqualTo(NSObject.FromObject(0));
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(logoWidth));
                m.Width.EqualTo(NSObject.FromObject(logoWidth));
            });
            logoButton.TouchUpInside += async(s, e) =>
            {
                bool r = await MyPopupManager.ShowYesNo("是否退出登录", new string[] { "取消", "确定" }, (sel) =>
                {
                    logger.Log("on sel", sel);
                });
                logger.Log("select", r);
                if (r)
                {
                    User.me.token = "";
                    User.me.Save();
                    headerBottomView.RemoveFromSuperview();
                    balanceView.RemoveFromSuperview();
                    createHeaderBottomView();
                    CreateBalance();
                }
            };

            var nickNameLabel = new UILabel();
            nickNameLabel.Text = User.me.name;
            nickNameLabel.TextColor = UIColor.White;
            nickNameLabel.Font = UIFont.BoldSystemFontOfSize(24);
            nickNameLabel.SetParent(userInfoView);
            nickNameLabel.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(logoWidth + 20));
                m.Top.EqualTo(NSObject.FromObject(25));
                m.Height.EqualTo(NSObject.FromObject(25));
            });

        }

        UIView balanceView;
        UILabel viewNumsValueLabel;
        UILabel balanceValueLabel;

        private void ResetUserInfo()
        {
            if (User.me.VideoCanPlayRemain > 0)
            {
                viewNumsValueLabel.Text = User.me.VideoCanPlayRemain + "";
                viewNumsValueLabel.Font = UIFont.SystemFontOfSize(18);
                viewNumsValueLabel.TextColor = UIColor.FromRGB(93, 93, 93);
            }
            else
            {
                viewNumsValueLabel.Text = "—";
                viewNumsValueLabel.TextColor = UIColor.FromRGB(204, 204, 204);
                viewNumsValueLabel.Font = UIFont.SystemFontOfSize(16);
            }
            if (User.me.balance > 0)
            {
                balanceValueLabel.Text = User.me.balance + "";
                balanceValueLabel.Font = UIFont.SystemFontOfSize(18);
                balanceValueLabel.TextColor = UIColor.FromRGB(93, 93, 93);
            }
            else
            {
                balanceValueLabel.Text = "—";
                balanceValueLabel.TextColor = UIColor.FromRGB(204, 204, 204);
                balanceValueLabel.Font = UIFont.SystemFontOfSize(16);
            }
            duedateLabel.Text = User.me.VipDueDate;
        }
        private void CreateBalance()
        {
            balanceView = new UIView();
            balanceView.SetParent(bkView);
            balanceView.BackgroundColor = UIColor.FromRGB(252, 252, 252);
            balanceView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(10));
                m.Top.EqualTo(NSObject.FromObject(130));
                m.Right.EqualTo(NSObject.FromObject(-10));
                m.Height.EqualTo(NSObject.FromObject(85));
            });
            balanceView.Layer.CornerRadius = 6;
            balanceView.ClipsToBounds = true;
            balanceView.Layer.BorderWidth = 0;

            var balanceLeftView = new UIView();
            balanceLeftView.SetParent(balanceView);
            balanceLeftView.MakeConstraints(m =>
            {
                m.Left.EqualTo(balanceView);
                m.Top.EqualTo(balanceView);
                m.Bottom.EqualTo(balanceView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width / 2) - 9.5f));
            });

            viewNumsValueLabel = new UILabel();
            viewNumsValueLabel.TextAlignment = UITextAlignment.Center;
            viewNumsValueLabel.Frame = new CGRect(0, 0, View.Bounds.Width / 2, 30);

            viewNumsValueLabel.SetParent(balanceLeftView);
            viewNumsValueLabel.MakeConstraints(m =>
            {
                m.Left.EqualTo(balanceLeftView);
                m.Top.EqualTo(NSObject.FromObject(-15));
                m.Bottom.EqualTo(balanceLeftView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width / 2) - 9.5f));
            });

            var viewNumsLabel = new UILabel();
            viewNumsLabel.Text = "剩余观看集数";
            viewNumsLabel.TextAlignment = UITextAlignment.Center;
            viewNumsLabel.Frame = new CGRect(0, 50, View.Bounds.Width / 2 - 9.5f, 30);
            viewNumsLabel.TextColor = UIColor.FromRGB(93, 93, 93);
            viewNumsLabel.Font = UIFont.SystemFontOfSize(16);
            viewNumsLabel.SetParent(balanceLeftView);

            var balanceMiddleView = new UIView();
            balanceMiddleView.SetParent(balanceView);
            balanceMiddleView.BackgroundColor = UIColor.FromRGB(238, 238, 238);
            balanceMiddleView.MakeConstraints(m =>
            {
                m.Left.EqualTo(balanceLeftView.Right());
                m.Top.EqualTo(NSObject.FromObject(19));
                m.Width.EqualTo(NSObject.FromObject(1));
                m.Height.EqualTo(NSObject.FromObject(50));
            });

            var balanceRightView = new UIView();
            balanceRightView.SetParent(balanceView);
            balanceRightView.MakeConstraints(m =>
            {
                m.Left.EqualTo(balanceMiddleView.Right());
                m.Top.EqualTo(balanceView);
                m.Bottom.EqualTo(balanceView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width / 2) - 9.5f));
            });

            balanceValueLabel = new UILabel();
            if (User.me.balance > 0)
            {
                balanceValueLabel.Text = User.me.balance + "";
                balanceValueLabel.Font = UIFont.SystemFontOfSize(18);
                balanceValueLabel.TextColor = UIColor.FromRGB(93, 93, 93);
            }
            else
            {
                balanceValueLabel.Text = "—";
                balanceValueLabel.TextColor = UIColor.FromRGB(204, 204, 204);
                balanceValueLabel.Font = UIFont.SystemFontOfSize(16);
            }
            balanceValueLabel.TextAlignment = UITextAlignment.Center;
            balanceValueLabel.Frame = new CGRect(0, 0, View.Bounds.Width / 2, 30);
            balanceValueLabel.SetParent(balanceRightView);
            balanceValueLabel.MakeConstraints(m =>
            {
                m.Left.EqualTo(balanceRightView);
                m.Top.EqualTo(NSObject.FromObject(-15));
                m.Bottom.EqualTo(balanceRightView);
                m.Width.EqualTo(NSObject.FromObject((View.Bounds.Width / 2) - 9.5f));
            });

            var balanceLabel = new UILabel();
            balanceLabel.Text = "可提现（元）";
            balanceLabel.TextAlignment = UITextAlignment.Center;
            balanceLabel.Frame = new CGRect(0, 50, View.Bounds.Width / 2 - 9.5f, 30);
            balanceLabel.TextColor = UIColor.FromRGB(93, 93, 93);
            balanceLabel.Font = UIFont.SystemFontOfSize(16);
            balanceLabel.SetParent(balanceRightView);

            // var balanceRight2View = new UIView();
            // balanceRight2View.SetParent(balanceView);
            // balanceRight2View.MakeConstraints(m =>
            // {
            //     m.Left.EqualTo(NSObject.FromObject((View.Bounds.Width / 4) * 3 + 25));
            //     m.Top.EqualTo(NSObject.FromObject(27));
            //     m.Width.EqualTo(NSObject.FromObject(35));
            //     m.Height.EqualTo(NSObject.FromObject(30));
            // });
            // var cashWithdrawalButton = new UIButton();
            // cashWithdrawalButton.Frame = new CGRect(0, 6, 35, 25);
            // cashWithdrawalButton.SetParent(balanceRight2View);
            // var cashWithdrawalLabel = new UILabel();
            // cashWithdrawalLabel.Text = "提现";
            // cashWithdrawalLabel.Frame = new CGRect(0, 0, 35, 25);
            // cashWithdrawalLabel.TextColor = UIColor.FromRGB(60, 60, 210);
            // cashWithdrawalLabel.Font = UIFont.SystemFontOfSize(13);
            // cashWithdrawalLabel.SetParent(cashWithdrawalButton);

            // cashWithdrawalButton.TouchUpInside += (s, e) =>
            // {
            //     logger.Log("开始提现====>");
            // };
        }
        private void CreateRecharge()
        {
            var adverisePicView = new UIView();
            adverisePicView.SetParent(bkView);
            adverisePicView.BackgroundColor = UIColor.FromRGB(252, 252, 252);
            adverisePicView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(10));
                m.Top.EqualTo(NSObject.FromObject(230));
                m.Width.EqualTo(NSObject.FromObject(View.Bounds.Width - 20));
                m.Height.EqualTo(NSObject.FromObject(140));
            });
            // var adImg = libmp.DataStore.store.onlineConfig.adImages[0];
            // Util.createAdverse(adImg, adverisePicView);
        }

        UILabel duedateLabel;

        private void CreateOperation()
        {
            var operationView = new UIView();
            operationView.SetParent(bkView);
            operationView.BackgroundColor = UIColor.FromRGB(252, 252, 252);
            operationView.MakeConstraints(m =>
            {
                m.Left.EqualTo(NSObject.FromObject(0));
                m.Top.EqualTo(NSObject.FromObject(382));
                m.Right.EqualTo(NSObject.FromObject(0));
                m.Height.EqualTo(NSObject.FromObject(194));
            });

            var btnPicWidth = 18;
            var btnPicHeight = 18;
            var viewHeight = 48;

            var OperationArray = new String[4][]
            {
                new String[] { "Lishijilu.png", "历史记录" },
                new String[] { "Downloadcache.png", "离线缓存" },
                new String[] { "Membershipexpires.png", "会员到期日" },
                new String[] { "Passwordmodification.png", "密码修改" }
            };
            var operationButtons = new UIButton[4]
            {
                new UIButton(),
                new UIButton(),
                new UIButton(),
                new UIButton(),
            };

            for (int i = 0; i < OperationArray.Length; i++)
            {
                var operationOneView = new UIView();
                operationOneView.SetParent(operationView);
                operationOneView.MakeConstraints(m =>
                {
                    m.Left.EqualTo(NSObject.FromObject(10));
                    m.Right.EqualTo(NSObject.FromObject(-10));
                    m.Top.EqualTo(NSObject.FromObject((viewHeight + 0.5f) * i));
                    m.Height.EqualTo(NSObject.FromObject(viewHeight));
                });

                operationButtons[i].SetParent(operationOneView);
                operationButtons[i].MakeConstraints(m =>
                {
                    m.Left.EqualTo(NSObject.FromObject(0));
                    m.Right.EqualTo(NSObject.FromObject(0));
                    m.Top.EqualTo(NSObject.FromObject(0));
                    m.Bottom.EqualTo(NSObject.FromObject(0));
                });

                var operationOneImageViewButton = new UIImageView();
                var imgoperationOne = UIImage.FromFile($"{App.HotStorageRoot}/res/user/{OperationArray[i][0]}");
                operationOneImageViewButton.Image = new UIImage(imgoperationOne.CGImage, 2, UIImageOrientation.Up);
                operationOneImageViewButton.SetParent(operationButtons[i]);
                operationOneImageViewButton.MakeConstraints(m =>
                {
                    m.CenterY.EqualTo(operationOneView.CenterY());
                    m.Left.EqualTo(NSObject.FromObject(0));
                    m.Height.EqualTo(NSObject.FromObject(btnPicHeight));
                    m.Width.EqualTo(NSObject.FromObject(btnPicWidth));
                });

                var operationOneLabel = new UILabel();
                operationOneLabel.Text = OperationArray[i][1];
                operationOneLabel.Frame = new CGRect(0, 35, View.Bounds.Width / 3, 25);
                operationOneLabel.TextColor = UIColor.FromRGB(93, 93, 93);
                operationOneLabel.Font = UIFont.SystemFontOfSize(16);
                operationOneLabel.SetParent(operationButtons[i]);
                operationOneLabel.MakeConstraints(m =>
                {
                    m.CenterY.EqualTo(operationOneView.CenterY());
                    m.Left.EqualTo(NSObject.FromObject(btnPicWidth + 10));
                    m.Height.EqualTo(NSObject.FromObject(20));
                    m.Right.EqualTo(operationOneView);
                });

                if (i == 2)
                {
                    duedateLabel = new UILabel();
                    duedateLabel.SetParent(operationOneView);
                    duedateLabel.TextColor = UIColor.FromRGB(93, 93, 93);
                    duedateLabel.Font = UIFont.SystemFontOfSize(16);
                    duedateLabel.TextAlignment = UITextAlignment.Right;
                    duedateLabel.MakeConstraints(m =>
                    {
                        m.CenterY.EqualTo(operationOneView.CenterY());
                        m.Right.EqualTo(NSObject.FromObject(0));
                        m.Height.EqualTo(NSObject.FromObject(btnPicHeight));
                        m.Width.EqualTo(NSObject.FromObject(150));
                    });
                    duedateLabel.Text = User.me.VipDueDate;

                    // DateTime dt = DateTime.Now;
                    // DateTime dt1 = Convert.ToDateTime("2019-04-29");
                    // int compNum = DateTime.Compare(dt, dt1);
                    // if (compNum >= 0)
                    // {
                    //     duedateLabel.Text = dt.ToString("yyyy-MM-dd");
                    // }
                    // else
                    // {
                    //     duedateLabel.Text = dt.ToString();
                    // }
                    // duedateLabel.Text = dt.ToString("yyyy-MM-dd");
                }
                else
                {
                    var operationArrowImageViewButton = new UIImageView();
                    var imgoperationArrow = UIImage.FromFile($"{App.HotStorageRoot}/res/user/Fanhui1.png");
                    operationArrowImageViewButton.Image = new UIImage(imgoperationArrow.CGImage, 2, UIImageOrientation.Up);
                    operationArrowImageViewButton.SetParent(operationOneView);
                    operationArrowImageViewButton.MakeConstraints(m =>
                    {
                        m.CenterY.EqualTo(operationOneView.CenterY());
                        m.Right.EqualTo(NSObject.FromObject(0));
                        m.Height.EqualTo(NSObject.FromObject(btnPicHeight));
                        m.Width.EqualTo(NSObject.FromObject(btnPicWidth));
                    });
                }

                var operationOneLineView = new UIView();
                operationOneLineView.SetParent(operationView);
                operationOneLineView.BackgroundColor = UIColor.FromRGB(238, 238, 238);
                operationOneLineView.MakeConstraints(m =>
                {
                    m.Left.EqualTo(NSObject.FromObject(10));
                    m.Right.EqualTo(NSObject.FromObject(-10));
                    m.Top.EqualTo(NSObject.FromObject(viewHeight * (i + 1) + 0.5f * i));
                    m.Height.EqualTo(NSObject.FromObject(0.5f));
                });
            }

            operationButtons[0].TouchUpInside += (s, e) =>
            {
                logger.Log("点击历史记录");
                AppDelegate.Instance.NavController.PushViewController(new VCHistoryRecordController(), false);
            };
            operationButtons[1].TouchUpInside += (s, e) =>
            {
                logger.Log("点击离线缓存");
                AppDelegate.Instance.NavController.PushViewController(new VCOfflineCacheController(), false);
            };
            operationButtons[3].TouchUpInside += (s, e) =>
            {
                logger.Log("点击密码修改");
                //测试弹窗
                var width = (float) (View.Bounds.Width * 0.8);
                var height = (float) (View.Bounds.Height * 0.5);
                var popView = Util.createPopView(width, height);
                MyPopupManager.ShowPopView(popView, width, height);
            };
            operationButtons[2].TouchUpInside += (s, e) =>
            {
                //测试会员中心入口
                AppDelegate.Instance.NavController.PushViewController(new VCMemberCenterController(), false);
            };
        }
        string baseUrl = App.CurrentServerUrl;

        public void UserRegister(string telephone, string nickName, string verificationCode, string email, string password, Action<LoginAndRegisterRequestInfo> onFinish)
        {
            var json = new { id = User.me.id, phone = telephone, name = nickName, email = email, password = Util.MD5Encrypt32(password) };
            var body = LitJson.JsonMapper.ToJson(json);
            var query = "";
            if (verificationCode.Length > 0)
            {
                query = "?verifyCode=" + verificationCode;
            }
            var url = $"http://{baseUrl}/user/register" + query;
            var res = Http.MakeRequest(new Http.Option
            {
                Url = url,
                    Method = HttpMethod.Post,
                    Body = body,
                    OnFinish = (ok, code, msg) =>
                    {
                        LoginAndRegisterRequestInfo requestInfo = new LoginAndRegisterRequestInfo();
                        logger.Log("finish async", url, ok, code, msg);
                        if (code == 200)
                        {
                            requestInfo = LitJson.JsonMapper.ToObject<LoginAndRegisterRequestInfo>(msg);
                        }
                        else
                        {
                            requestInfo.error = "系统出错，请重试";
                        }

                        logger.Log(requestInfo.msg + "---->" + requestInfo.error + "---->" + requestInfo.genVerifyCode + "---->" + requestInfo.token);
                        onFinish(requestInfo);
                    },
                    OnProgress = (total, now) =>
                    {
                        logger.Log("progress", total, now);
                    }
            });
        }

        public void UserLogin(string telephone, string password, Action<UserInfoData> onFinish)
        {
            var lastid = User.me.id;
            var body = LitJson.JsonMapper.ToJson(new { phone = telephone, password = Util.MD5Encrypt32(password) });
            var url = $"http://{baseUrl}/user/login";
            var res = Http.MakeRequest(new Http.Option
            {
                Url = url,
                    Method = HttpMethod.Post,
                    Body = body,
                    OnFinish = (ok, code, msg) =>
                    {
                        UserInfoData requestInfo = new UserInfoData();
                        logger.Log("login finish async", url, ok, code, msg);
                        if (code == 200)
                        {
                            requestInfo = LitJson.JsonMapper.ToObject<UserInfoData>(msg);
                        }
                        else
                        {
                            requestInfo.error = "系统出错，请重试";
                        }

                        logger.Log(requestInfo.msg + "---->" + requestInfo.error + "---->" + requestInfo.token + "---->" + requestInfo.id);

                        if (requestInfo.msg.Length > 0)
                        {
                            // User.me.Update(requestInfo);
                            User.me.Notify("login", LitJson.JsonMapper.ToJson(new
                            {
                                lastid = lastid,
                            }));
                        }
                        onFinish(requestInfo);
                    },
                    OnProgress = (total, now) =>
                    {
                        logger.Log("progress", total, now);
                    }
            });
        }

        public void updateUserInfoView()
        {
            headerBottomView.RemoveFromSuperview();
            balanceView.RemoveFromSuperview();
            createHeaderBottomView();
            CreateBalance();
            duedateLabel.Text = User.me.VipDueDate;
        }

    }
}